//
//  LogisticsInfoViewController.m
//  OMS
//
//  Created by goldeneye on 14/11/4.
//  Copyright (c) 2014年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "CRECOMLogisticsInfoYJController.h"
#import "OMLogisticsInfoViewCell.h"
#import "WebServiceCommonRequest.h"
#import "MJRefresh.h"

#import "UITableView+Custom.h"
#import "NSDictionary+DataFormat.h"


@interface CRECOMLogisticsInfoYJController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) NSMutableArray *resultData;
@property (nonatomic,strong) NSMutableDictionary *orderBaseInfo;

@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property (nonatomic, strong) IBOutlet UIView *headerView;

@property (nonatomic, strong) IBOutlet UILabel *DHDBHLable;
@property (nonatomic, strong) IBOutlet UILabel *HTMCLable;
@property (nonatomic, strong) IBOutlet UILabel *GCMCLable;
@property (nonatomic, strong) IBOutlet UILabel *SGBWLable;
@property (nonatomic, strong) IBOutlet UILabel *FHZDLable;
@property (nonatomic, strong) IBOutlet UILabel *JHFLLable;
@property (nonatomic, strong) IBOutlet UILabel *logisticsaLable;

@property (nonatomic, strong) IBOutlet UILabel *QDDJLable;
@property (nonatomic, strong) IBOutlet UILabel *TLDLable;
@property (nonatomic, strong) IBOutlet UILabel *JZFSLable;
@property (nonatomic, strong) IBOutlet UILabel *JHKPSJLable;

@end

@implementation CRECOMLogisticsInfoYJController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle: @"罐车发货信息"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView registerClass:[OMLogisticsInfoViewCell class] forCellReuseIdentifier:@"OMLogisticsInfoViewCell"];
    
    NSString *DHDBH = [self.orderBaseInfo objectFormatStringForKey:@"订单编号" defaults:@"未知"];
    NSString *SGBW = [self.orderBaseInfo objectFormatStringForKey:@"施工部位" defaults:@"未知"];
    NSString *JHFL = [self.orderBaseInfo objectFormatStringForKey:@"计划方量" format:@"%@方" defaults:@"未知"];
    self.DHDBHLable.text = DHDBH;
    self.SGBWLable.text = SGBW;
    self.JHFLLable.text = JHFL;
    
    self.HTMCLable.text = [self.orderBaseInfo objectFormatStringForKey:@"合同名称" defaults:@"未知"];
    self.GCMCLable.text = [self.orderBaseInfo objectFormatStringForKey:@"工程名称" defaults:@"未知"];
    self.FHZDLable.text = [self.orderBaseInfo objectFormatStringForKey:@"实际分发站" defaults:@"未知"];
    
    self.QDDJLable.text = [self.orderBaseInfo objectFormatStringForKey:@"砼强度" defaults:@"未知"];
    self.TLDLable.text = [self.orderBaseInfo objectFormatStringForKey:@"坍落度" defaults:@"未知"];
    self.JZFSLable.text = [self.orderBaseInfo objectFormatStringForKey:@"浇筑方式" defaults:@"未知"];
    self.JHKPSJLable.text = [self.orderBaseInfo objectFormatStringForKey:@"计划开盘时间" defaults:@"未知"];
    
//    NSInteger DDZTint = [self.orderBaseInfo objectFormatIntegerForKey:@"DDZT"];
//    if(DDZTint == 1) self.logisticsaLable.text = @"订单已审核，正在发货中";
//    else self.logisticsaLable.text = @"订单正在审核，等待发货中";
    
    NSInteger fffl = [self.orderBaseInfo objectFormatIntegerForKey:@"发货方量"];
    NSInteger ffcc = [self.orderBaseInfo objectFormatIntegerForKey:@"发货车次"];
    if(ffcc>0) self.logisticsaLable.text = [NSString stringWithFormat:@"累计发货%ld方，已发%ld车",fffl,ffcc];
    else self.logisticsaLable.text = @"正等待发货";
    
    self.resultData = [NSMutableArray array];
    [self setupRefresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getPassingParameter:(id)object
{
    NSMutableDictionary *dic = object;
    self.orderBaseInfo = dic;
}

- (void)setupRefresh
{
    //添加下拉加载数据
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    [self.tableView headerBeginRefreshing];
    [self.tableView footerEndRefreshing];
}

- (void)stopRefresh
{
    //移除下拉加载数据
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
}

#pragma mark 开始进入刷新状态

- (void)headerRereshing
{
    NSNumber *stateNumber = [NSNumber numberWithInteger:1];
    NSNumber *endNumber = [NSNumber numberWithInteger:10];
    [self getNetWorkDataIsReresh:YES state:stateNumber end:endNumber];
}

- (void)footerRereshing
{
    NSNumber *stateNumber = [NSNumber numberWithInteger:self.resultData.count + 1];
    NSNumber *endNumber = [NSNumber numberWithInteger:self.resultData.count + 10];
    [self getNetWorkDataIsReresh:NO state:stateNumber end:endNumber];
}

#pragma mark 网络获取物流信息
- (void)getNetWorkDataIsReresh:(BOOL)isReresh state:(NSNumber*)state end:(NSNumber*)end
{
    NSString *orderID = [self.orderBaseInfo objectFormatStringForKey:@"ID"];
    
    //@审核状态（0未审核，1已审核）
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"DDGL_FHD_LIST"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content addParameterKey:@"起始条数" value:state];
    [content addParameterKey:@"结束条数" value:end];
    [content addParameterKey:@"订单ID" value:orderID];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if(isReresh) [self.tableView headerEndRefreshing];
        else [self.tableView footerEndRefreshing];
        
        //添加数据，刷新页面
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0)
            firstArray = [result.resultArray firstObject];
        
        if(isReresh) [self.resultData removeAllObjects];
        if(firstArray && firstArray.count>0){
            [self.resultData addObjectsFromArray:firstArray];
            [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
            [self.tableView reloadData];
        } else if(isReresh){
            [self.tableView setTableViewNotDataDrawText:@"未找物流信息" isShow:YES];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        if(isReresh)[self errorInfoViewText:@"哦，出错了"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        if(isReresh)[self errorInfoViewText:@"无法连接网络"];
    }];
    
}

- (void)errorInfoViewText:(NSString*)label
{
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
    
    if (self.tableView.visibleCells.count <= 0) {
        [self.tableView setTableViewNotDataDrawText:label isShow:YES];
    } else {
        [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
        [self showErrorHUDText:label];
    }
}

#pragma mark UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 30)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, SuperWidth - 10, 29.5)];
    lable.textColor = ThemeColorBlue;
    lable.font = [UIFont boldSystemFontOfSize:15];
    lable.text = [NSString stringWithFormat:@"物流详情"];
    [view addSubview:lable];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 29.5, SuperWidth, 0.5)];
    lineView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
    [view addSubview:lineView];
    
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.resultData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"OMLogisticsInfoViewCell";
    OMLogisticsInfoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    NSDictionary *itemInfo = [self.resultData objectAtIndex:indexPath.row];
    cell.driverLabel.text = [itemInfo objectFormatStringForKey:@"司机"];
    cell.startTimeLabel.text = [itemInfo objectFormatStringForKey:@"创建时间"];
    cell.licenseNumberLabel.text = [itemInfo objectFormatStringForKey:@"车牌号" defaults:@"未收录"];
    cell.capacityLabel.text = [itemInfo objectFormatStringForKey:@"发货方量" format:@"容量%@方" defaults:@"未知" ];
    
    if (indexPath.row == 0) {
        cell.numberImageView.hidden = NO;
        cell.numberLabel.hidden = YES;
    } else {
        cell.numberImageView.hidden = YES;
        cell.numberLabel.hidden = NO;
        cell.numberLabel.text = [itemInfo objectFormatStringForKey:@"第几车" defaults:@"0"];
    }
    return cell;
}

#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 73;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark -
#pragma mark IBAction Event
- (void)backButtonEvent:(id)sender
{
    if (self.backResultDelagate && [self.backResultDelagate respondsToSelector:@selector(backResultDataKey:dataValue:)]) {
        [self.backResultDelagate backResultDataKey:self.key dataValue:nil];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

@end
